package com.huan.web;

import com.alibaba.fastjson.JSON;
import com.huan.pojo.PageBean;

import com.huan.pojo.Teacher;

import com.huan.service.TeacherService;

import com.huan.service.impl.TeacherServiceImpl;
import com.huan.web.base.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;


@WebServlet("/teacher/*")
public class TeacherServlet extends BaseServlet {
    TeacherService service = new TeacherServiceImpl();

    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws Exception
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        //转为对象
        Teacher teacher = JSON.parseObject(params, Teacher.class);
 /*       if (book == null) {
            book = new Book();
        }*/

        PageBean<Teacher> pageBean = service.selectByPageAndCondition(currentPage, pageSize, teacher);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 增加
     * @param request
     * @param response
     * @throws Exception
     */
    public void add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);
        Teacher teacher = JSON.parseObject(s, Teacher.class);
        System.out.println(teacher);


        boolean flag = service.add(teacher);
        if (flag){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }

    }

    /**
     * 编辑数据时传过来的id（有无都可）
     * @param request
     * @param response
     * @throws Exception
     */
    public void acceptId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        System.out.println("浏览器传过来的："+params);

/*        String acceptId = JSON.parseObject(params, String.class);
        System.out.println("存进session的"+acceptId);*/

        HttpSession session = request.getSession();
        session.setAttribute("acceptId",params);

        response.getWriter().write("success");

    }

    /***
     * 编辑
     * @param request
     * @param response
     * @throws Exception
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

      /*  HttpSession session = request.getSession();
        String acceptId = (String) session.getAttribute("acceptId");
        System.out.println(acceptId);*/

        Teacher teacher = JSON.parseObject(params, Teacher.class);


        /* student.setStudentID(acceptId);*/
        System.out.println(teacher);

        int i = service.update(teacher);
        if (i>0){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }
    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws Exception
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        System.out.println("---------"+params+"---------");

        service.delete(params);

        response.getWriter().write("success");

    }

    /**
     * 批量删除
     * @param request
     * @param response
     * @throws Exception
     */
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        System.out.println("批量删除读出来的："+params);

        //转为数组
        String[]  ids = JSON.parseObject(params, String[].class);
        System.out.println(ids.toString());

        service.deleteByIds(ids);

        response.getWriter().write("success");
    }
}
